package com.woniu.gf.pawn.config;

import com.woniu.gf.pawn.dto.DocketDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject: gf-pawn-system
 * @BelongsPackage: com.woniu.gf.pawn.web.config
 * @Author: wu wei
 * @CreateTime: 2020-10-16 21:20
 * @Description:
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(DocketDto.class)
public class WebConfig {
    @Autowired
    private DocketDto docketDto;
    @Bean
    public Docket createDocket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(new ApiInfoBuilder()
                .title(docketDto.getTitle())
                .description(docketDto.getDescription())
                .contact(new Contact(docketDto.getAuthor().getName(),docketDto.getAuthor().getUrl(),docketDto.getAuthor().getEmail()))
                .version(docketDto.getVersion())
                .build()
        )
                .globalOperationParameters(globalOperation())
                .select()
                .apis(RequestHandlerSelectors.basePackage(docketDto.getBasePath()))
                .paths(PathSelectors.any())
                .build()
//                .globalOperationParameters(globalOperation())
        ;
        return docket;
    }

    private List<Parameter> globalOperation(){
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        //第一个token为传参的key，第二个token为swagger页面显示的值
        tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
}
